import Vue from 'vue'
import App from './App.vue'
import VCharts from 'v-charts'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

Vue.use(VCharts)
Vue.config.productionTip = false

window.vm = new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')// vue2.6.5
// render: h=>h(App)
// render:function(h){return h(App)}
// render:h=>{return h(App)}
// render:h=>h(App)


import axios from 'axios'
axios.defaults.baseURL = 'http://localhost:8081'
let $api = (option)=>{
  let method = option.method
  method || (method = 'GET')
  method = method.toUpperCase()
  let headers = option.headers || {}


  // let loading
  // if (!option.hide) {
  //   loading = window.vm.$loading({
  //     lock: true,
  //     text: 'Loading',
  //     spinner: 'el-icon-loading',
  //     background: 'rgba(0, 0, 0, 0.7)'
  //   });
  // }
  axios({
    url:option.url,// /users/login
    method,
    data:method=='POST'&&option.data,
    params:method=='GET'&&option.data,
    headers:{token:window.localStorage.token,...headers},
  }).then(({data})=>{
    // if (!option.hide && loading) {
    //   loading.close()
    // }
    if (option.success) {
      option.success(data)
    }
  }).catch((err)=>{
    // if (option.error) {
    //   option.error(err)
    // }
    console.log(err)
    // if (!option.hide && loading) {
    //   loading.close()
    // }
    // window.vm.$message.error('请求异常')
  })
}

// Vue.use($api) //注册挂载
Vue.prototype.$api = $api //注册
